{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.1.0\n",
      "sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)\n",
      "matplotlib 3.1.3\n",
      "numpy 1.18.1\n",
      "pandas 1.0.1\n",
      "sklearn 0.22.1\n",
      "tensorflow 2.1.0\n",
      "tensorflow_core.python.keras.api._v2.keras 2.2.4-tf\n"
     ]
    }
   ],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sklearn\n",
    "import pandas as pd\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import tensorflow as tf\n",
    "\n",
    "from tensorflow import keras\n",
    "\n",
    "print(tf.__version__)\n",
    "print(sys.version_info)\n",
    "for module in mpl, np, pd, sklearn, tf, keras:\n",
    "    print(module.__name__, module.__version__)\n",
    "\n",
    "'''\n",
    "1. 将一般的python函数转化为tf.function形式，在能够同样调用的同时，获取更高的效率\n",
    "    使用 functionObj = tf.function(函数名) 转化\n",
    "    使用标注的形式转化\n",
    "    \n",
    "2. 使用标注的函数，可以指定tf.function 的签名\n",
    "    在指定签名之后，就可以得到具体函数\n",
    "    cube_func_int32 = cube.get_concrete_function(\n",
    "    tf.TensorSpec([None], tf.int32))\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor([-0.63212055  0.         -0.95021296], shape=(3,), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "## 如果features小于0,计算指数线性：exp(features) - 1,否则为features.\n",
    "z=tf.constant([-1.,0,-3])\n",
    "print(tf.nn.elu(z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(3.0, shape=(), dtype=float32)\n",
      "tf.Tensor([-0.95021296 -0.917915  ], shape=(2,), dtype=float32)\n",
      "tf.Tensor(-0.95021296, shape=(), dtype=float32)\n",
      "tf.Tensor([-0.95021296 -0.917915  ], shape=(2,), dtype=float32)\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# tf.function and auto-graph.\n",
    "## 将一般的函数转化为tf.function，转化为tf.function（进而转化为 concrete_function具体函数），\n",
    "## 就能够被作为文件保存下来，在其他地方调用；同时效率也高 \n",
    "def scaled_elu(z, scale=1.0, alpha=1.0):\n",
    "    # z >= 0 ? scale * z : scale * alpha * tf.nn.elu(z)\n",
    "    \n",
    "    is_positive = tf.greater_equal(z, 0.0)\n",
    "    ## tg.nn.elu 是个激活函数，如果features小于0,计算指数线性：exp(features) - 1,否则为features.\n",
    "    return scale * tf.where(is_positive, z, alpha * tf.nn.elu(z))\n",
    "\n",
    "print(scaled_elu(tf.constant(3.)))\n",
    "print(scaled_elu(tf.constant([-3., -2.5])))\n",
    "\n",
    "## 使用 tf.function，传入函数。即可将函数转化为tf.function\n",
    "scaled_elu_tf = tf.function(scaled_elu)\n",
    "\n",
    "## 可见，将函数转化为tf.function时，函数的功能性上没有变化\n",
    "print(scaled_elu_tf(tf.constant(-3.)))\n",
    "print(scaled_elu_tf(tf.constant([-3., -2.5])))\n",
    "\n",
    "## 可以看到，tf.function 对象有个成员变量，python_function ，就是一般的python函数\n",
    "print(scaled_elu_tf.python_function is scaled_elu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25.4 ms ± 1.75 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
      "23.5 ms ± 454 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "## 测试时间，可以发现转化为tf.function，函数执行的效率高了很多，特别是在gpu中\n",
    "%timeit scaled_elu(tf.random.normal((1000, 1000)))\n",
    "%timeit scaled_elu_tf(tf.random.normal((1000, 1000)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(1.9999981, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# 1 + 1/2 + 1/2^2 + ... + 1/2^n\n",
    "\n",
    "## 通过添加 标注，将python函数变为 tf.function \n",
    "@tf.function\n",
    "def converge_to_2(n_iters):\n",
    "    total = tf.constant(0.)\n",
    "    increment = tf.constant(1.)\n",
    "    for _ in range(n_iters):\n",
    "        total += increment\n",
    "        increment /= 2.0\n",
    "    return total\n",
    "\n",
    "print(converge_to_2(20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_tf_code(func):\n",
    "    ## 当python函数添加标注变为 tf.function时，就会有很多方法可以调用\n",
    "    code = tf.autograph.to_code(func)\n",
    "    from IPython.display import display, Markdown\n",
    "    display(Markdown('```python\\n{}\\n```'.format(code)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "```python\n",
       "from __future__ import print_function\n",
       "\n",
       "def tf__scaled_elu(z, scale=None, alpha=None):\n",
       "  try:\n",
       "    with ag__.function_scope('scaled_elu'):\n",
       "      do_return = False\n",
       "      retval_ = None\n",
       "      is_positive = ag__.converted_call('greater_equal', tf, ag__.ConversionOptions(recursive=True, verbose=0, strip_decorators=(ag__.convert, ag__.do_not_convert, ag__.converted_call), force_conversion=False, optional_features=ag__.Feature.ALL, internal_convert_user_code=True), (z, 0.0), {})\n",
       "      do_return = True\n",
       "      retval_ = scale * ag__.converted_call('where', tf, ag__.ConversionOptions(recursive=True, verbose=0, strip_decorators=(ag__.convert, ag__.do_not_convert, ag__.converted_call), force_conversion=False, optional_features=ag__.Feature.ALL, internal_convert_user_code=True), (is_positive, z, alpha * tf.nn.elu(z)), {})\n",
       "      return retval_\n",
       "  except:\n",
       "    ag__.rewrite_graph_construction_error(ag_source_map__)\n",
       "\n",
       "\n",
       "\n",
       "tf__scaled_elu.autograph_info__ = {}\n",
       "\n",
       "```"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_tf_code(scaled_elu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(21.0, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "var = tf.Variable(0.)\n",
    "\n",
    "@tf.function\n",
    "def add_21():\n",
    "    return var.assign_add(21) # += \n",
    "\n",
    "print(add_21())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python inputs incompatible with input_signature: inputs ((<tf.Tensor: id=1933, shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>,)), input_signature ((TensorSpec(shape=(None,), dtype=tf.int32, name='x'),))\n",
      "tf.Tensor([ 1  8 27], shape=(3,), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "## 在标注函数的过程中，在标注中指定函数的 输入参数的数据格式\n",
    "## [None]表示对shape不限定\n",
    "@tf.function(input_signature=[tf.TensorSpec([None], tf.int32, name='x')])\n",
    "def cube(z):\n",
    "    return tf.pow(z, 3)\n",
    "\n",
    "## 不按照签名调用函数，就会报错\n",
    "try:\n",
    "    print(cube(tf.constant([1., 2., 3.])))\n",
    "except ValueError as ex:\n",
    "    print(ex)\n",
    "\n",
    "\n",
    "print(cube(tf.constant([1, 2, 3])))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tensorflow.python.eager.function.ConcreteFunction object at 0x135884518>\n"
     ]
    }
   ],
   "source": [
    "# @tf.function py func -> tf graph\n",
    "# get_concrete_function -> add input signature -> SavedModel\n",
    "\n",
    "## 标注过签名的tf.function 可以调用get_concrete_function ，得到具体函数\n",
    "## 具体函数的作用就是：这个函数能够被保存下来，保存为TFLite Converter\n",
    "\n",
    "## get_concrete_function 需要传入签名 或者 满足签名的数据格式\n",
    "cube_func_int32 = cube.get_concrete_function(\n",
    "    tf.TensorSpec([None], tf.int32))\n",
    "print(cube_func_int32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(cube_func_int32 is cube.get_concrete_function(\n",
    "    tf.TensorSpec([5], tf.int32)))\n",
    "print(cube_func_int32 is cube.get_concrete_function(\n",
    "    tf.constant([1, 2, 3])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.framework.func_graph.FuncGraph at 0x1351840b8>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 具体函数查看图结构\n",
    "cube_func_int32.graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<tf.Operation 'x' type=Placeholder>,\n",
       " <tf.Operation 'Pow/y' type=Const>,\n",
       " <tf.Operation 'Pow' type=Pow>,\n",
       " <tf.Operation 'Identity' type=Identity>]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 获取concrete_function 图结构的 内部操作\n",
    "cube_func_int32.graph.get_operations()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: \"Pow\"\n",
      "op: \"Pow\"\n",
      "input: \"x\"\n",
      "input: \"Pow/y\"\n",
      "attr {\n",
      "  key: \"T\"\n",
      "  value {\n",
      "    type: DT_INT32\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "pow_op = cube_func_int32.graph.get_operations()[2]\n",
    "print(pow_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<tf.Tensor 'x:0' shape=(None,) dtype=int32>, <tf.Tensor 'Pow/y:0' shape=() dtype=int32>]\n",
      "[<tf.Tensor 'Pow:0' shape=(None,) dtype=int32>]\n"
     ]
    }
   ],
   "source": [
    "print(list(pow_op.inputs))\n",
    "print(list(pow_op.outputs))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Operation 'x' type=Placeholder>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cube_func_int32.graph.get_operation_by_name(\"x\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor 'x:0' shape=(None,) dtype=int32>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cube_func_int32.graph.get_tensor_by_name(\"x:0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "node {\n",
       "  name: \"x\"\n",
       "  op: \"Placeholder\"\n",
       "  attr {\n",
       "    key: \"_user_specified_name\"\n",
       "    value {\n",
       "      s: \"x\"\n",
       "    }\n",
       "  }\n",
       "  attr {\n",
       "    key: \"dtype\"\n",
       "    value {\n",
       "      type: DT_INT32\n",
       "    }\n",
       "  }\n",
       "  attr {\n",
       "    key: \"shape\"\n",
       "    value {\n",
       "      shape {\n",
       "        dim {\n",
       "          size: -1\n",
       "        }\n",
       "      }\n",
       "    }\n",
       "  }\n",
       "}\n",
       "node {\n",
       "  name: \"Pow/y\"\n",
       "  op: \"Const\"\n",
       "  attr {\n",
       "    key: \"dtype\"\n",
       "    value {\n",
       "      type: DT_INT32\n",
       "    }\n",
       "  }\n",
       "  attr {\n",
       "    key: \"value\"\n",
       "    value {\n",
       "      tensor {\n",
       "        dtype: DT_INT32\n",
       "        tensor_shape {\n",
       "        }\n",
       "        int_val: 3\n",
       "      }\n",
       "    }\n",
       "  }\n",
       "}\n",
       "node {\n",
       "  name: \"Pow\"\n",
       "  op: \"Pow\"\n",
       "  input: \"x\"\n",
       "  input: \"Pow/y\"\n",
       "  attr {\n",
       "    key: \"T\"\n",
       "    value {\n",
       "      type: DT_INT32\n",
       "    }\n",
       "  }\n",
       "}\n",
       "node {\n",
       "  name: \"Identity\"\n",
       "  op: \"Identity\"\n",
       "  input: \"Pow\"\n",
       "  attr {\n",
       "    key: \"T\"\n",
       "    value {\n",
       "      type: DT_INT32\n",
       "    }\n",
       "  }\n",
       "}\n",
       "versions {\n",
       "  producer: 27\n",
       "}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cube_func_int32.graph.as_graph_def()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
